package data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.Activity;

/**
 * Creates a task that gets a list of items from a database
 * 
 * @author Kelsey
 */
public class GetItemList extends DBTask<Void, Void, List<Item>> {

	/** activity being used */
	private Activity activity;

	/**
	 * Constructs task to get a list of Items
	 * 
	 * @param activity
	 *            the current Android activity
	 * @throws DBInterfaceError
	 */
	public GetItemList(Activity activity) throws DBInterfaceError {
		super(activity);
		this.activity = activity;
	}

	/**
	 * gets the list of Items from a database
	 * 
	 * @return list of Items
	 * @throws DBInterfaceError
	 */
	@Override
	public List<Item> getResult() throws DBInterfaceError {
		execute();
		List<Item> items = null;
		try {
			items = get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

		return items;
	}

	/**
	 * Searches for and returns a list of items from a database
	 * 
	 * @return list of Items from database
	 */
	@Override
	protected List<Item> doInBackground(Void... arg0) {
		PreparedStatement getItemsQuery;
		List<Item> list = null;
		
		Session session = (Session) activity.getApplicationContext();

		String filterStatus = "";
		if (session.getFilterStatus().equals("open")) {
			filterStatus = "status='Open'";
		}
		else if (session.getFilterStatus().equals("resolved")) {
			filterStatus = "status='Resolved'";
		}

		if (!filterStatus.equals("")
				&& (session.getFilterHeirloomCategory()
						|| session.getFilterKeepsakeCategory() || session
							.getFilterMiscCategory())) {
			filterStatus += " AND ";
		}

		boolean includeOr = false;
		String filterCategory = "(";
		if (session.getFilterHeirloomCategory()) {
			filterCategory += "category='Heirloom'";
			includeOr = true;
		}
		if (session.getFilterKeepsakeCategory()) {
			if (includeOr) {
				filterCategory += " OR ";
			}
			filterCategory += "category='Keepsake'";
			includeOr = true;
		}
		if (session.getFilterMiscCategory()) {
			if (includeOr) {
				filterCategory += " OR ";
			}
			filterCategory += "category='Misc'";
		}
		filterCategory += ")";

		String where = "WHERE " + filterStatus + " " + filterCategory;
		String dateOrder = "DESC";
		if (!session.getFilterNewestDate()) {
			dateOrder = "ASC";
		}
		
		try {
			getItemsQuery = conn.prepareStatement("SELECT * FROM item_old "
					+ where + " ORDER BY dateEntered " + dateOrder);
			ResultSet set = getItemsQuery.executeQuery();
			
			list = new ArrayList<Item>();
			while (set.next()) {
				Item buff = new Item();
				buff.setAccountName(set.getString("accountName"));
				buff.setCategory((Item.Category.valueOf(set
						.getString("category"))));
				buff.setStatus((Item.Status.valueOf(set.getString("status"))));
				buff.setType((Item.ItemType.valueOf(set.getString("type"))));
				buff.setDescription(set.getString("description"));
				buff.setDateEntered(set.getTimestamp("dateEntered"));
				buff.setLocation(set.getString("location"));
				buff.setName(set.getString("name"));
				list.add(buff);
			}
		} catch (SQLException e) {
			System.err.println(e);
		}

		if (session.getSearchQuery() != ""
				&& session.getSearchQuery() != null) {
			ItemsSortByQuery sortItems = new ItemsSortByQuery(
					session.getSearchQuery());
			sortItems.sort(list);
		}

		return list;
	}

}
